import {defineStore} from "pinia";
import {addCollect, delCollect, findAllCollect, getCollect, updateCollect} from "@/api/collect";

export const CollectStore = defineStore('collect', {
    state:()=>{
        return {
            // 列表
            collectList: [],
            // 详细信息
            collectInfo: {}
        }
    },
    actions:{
        addCollect(){
            return new Promise((resolve, reject)=>{
                addCollect(this.collectInfo).then(res => {
                    resolve(res.result)
                    this.collectInfo = {}
                }).catch(err => {
                    reject()
                })
            })
        },
        async getCollectList(query) {
            return new Promise((resolve, reject)=>{
                findAllCollect(query).then(res => {
                    this.collectList = res.result.list
                    resolve(res.result)
                }).catch(err => {
                    reject()
                })
            })
        },
        async getCollectInfo(id) {
            const {result} = await getCollect(id)
            this.collectInfo = result
            this.collectInfo.collect_price = parseFloat(this.collectInfo.collect_price)
        },
        updateCollect(id,data) {
            return new Promise(async (resolve, reject) => {
                updateCollect(id,data).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },
        async deleteCollect(id) {
            const {code} = await delCollect(id)
        },
    }
})